package com.qf.sl.youshare;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.Html;
import android.text.Spanned;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.TextView;

/**
 * Created by IDEA
 * User : sl
 * on 2015/2/25.
 * 18:09
 */
public class ShareContentActivity extends Activity {

    private WebView webView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_share_content);

        webView = (WebView) findViewById(R.id.share_content_web_view);

        ActionBar actionBar = getActionBar();

        if (actionBar != null) {

            initActionBar(actionBar);

        }

        Intent intent = getIntent();
        String string = intent.getStringExtra("content");

        string = string.replace("\\\"","\"");

        System.out.println("string = " + string);

        Spanned html = Html.fromHtml(string);
        String content = html.toString();

//        WebSettings settings = webView.getSettings();
//        settings.setDefaultTextEncodingName("UTF-8");

//        webView.loadData(content, "text/html", null);

        webView.loadDataWithBaseURL(null, content, "text/html", "UTF-8", null);

    }

    /**
     * 对ActionBar进行初始化操作
     * @param actionBar 要操作的ActionBar
     */
    private void initActionBar(ActionBar actionBar) {

        //设置返回按钮
        actionBar.setDisplayHomeAsUpEnabled(true);

        //去掉 ActionBar 上的图标
        actionBar.setIcon(new ColorDrawable(Color.TRANSPARENT));

        //标题居中
        actionBar.setDisplayShowCustomEnabled(true);

        //从 layout 文件夹中加载布局,然后通过代码修改标题
        LayoutInflater inflater = LayoutInflater.from(this);
        View view = inflater.inflate(R.layout.actionbar_main_title, null);

        //获取 view 中的 TextView 进行标题的设置
        TextView textView = (TextView) view.findViewById(R.id.actionbar_title);
        if (textView != null) {
            textView.setText("社交");
        }

        actionBar.setCustomView(
                view,
                new ActionBar.LayoutParams(
                        Gravity.CENTER)
        );
        actionBar.setBackgroundDrawable(new ColorDrawable(0xFFA38F74));

        actionBar.setDisplayShowTitleEnabled(false);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.main, menu);

        return true;
    }
}